package com.peng.leetcode.sort;

/**
 * SortColors
 *
 * @author: lupeng6
 * @create: 2020/12/10 17:12
 */
public class SortColors {

    public static void main(String[] args) {
//        int[] arr = {2, 0, 2, 1, 1, 0};
//        int[] arr = {1, 2, 0};
        int[] arr = {2, 1, 2};
        new Solution().sortColors(arr);
        for (int i : arr) {
            System.out.println(i);
        }
    }

    static class Solution {
        public void sortColors(int[] nums) {
            int l1 = 0;
            int l2 = nums.length - 1;
            int temp;
            for (int i = 0; i < nums.length; i++) {
                while (nums[i] == 2 && i <= l2) {
                    temp = nums[i];
                    nums[i] = nums[l2];
                    nums[l2] = temp;
                    l2--;
                }

                if (nums[i] == 0) {
                    temp = nums[i];
                    nums[i] = nums[l1];
                    nums[l1] = temp;
                    l1++;
                }
            }
        }
    }
}
